/*
 * Copyright (C) 2022 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http:// www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

#include "wifi_device.h"
#include "cmsis_os2.h"

#include "lwip/netifapi.h"

#if CHIP_WS63
#include "lwip/nettool/misc.h"
#else
#include "lwip/api_shell.h"
#endif

#define ZERO 0
#define ONE 1
#define TWO 2
#define THREE 3
#define FOUR 4
#define FIVE 5
#define TEN 10
#define ONE_HUNDRED 100

// 打印WiFi连接信息
static void PrintLinkedInfo(WifiLinkedInfo* info)
{
    if (!info) return;
}

// 全局变量，记录WiFi连接状态
static volatile int g_connected = 0;

// WiFI连接状态改变时的回调函数
static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
{
    if (!info) return;

//debug
#if 1
    printf("OnWifiConnectionChanged info: %p\r\n", info);
#endif

    printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
    PrintLinkedInfo(info);

    if (state == WIFI_STATE_AVAILABLE) {
        g_connected = 1;
    } else {
        g_connected = 0;
    }
}

// 定义一个OnWifiScanStateChanged函数，用于处理wifi扫描状态改变的事件
static void OnWifiScanStateChanged(int state, int size)
{
    // 打印函数名和行号
    printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
}

// 定义一个g_defaultWifiEventListener变量，用于保存默认的wifi事件监听器
static WifiEvent g_defaultWifiEventListener = {
    .OnWifiConnectionChanged = OnWifiConnectionChanged,
    .OnWifiScanStateChanged = OnWifiScanStateChanged
};

// 定义一个g_iface变量，用于保存网络接口
static struct netif* g_iface = NULL;

/*连接到AP*/
int ConnectToHotspot(WifiDeviceConfig* apConfig)
{
    WifiErrorCode errCode;
    int netId = -1;

    //注册WiFi事件监听器
    errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
    printf("RegisterWifiEvent: %d\r\n", errCode);

    //启用WiFi
    errCode = EnableWifi();
    printf("EnableWifi: %d\r\n", errCode);

    //添加设备配置
    errCode = AddDeviceConfig(apConfig, &netId);
    printf("AddDeviceConfig: %d\r\n", errCode);

    //连接到指定的网络ID
    g_connected = 0;
    errCode = ConnectTo(netId);
    printf("ConnectTo(%d): %d\r\n", netId, errCode);

    //等待连接到AP
    while (!g_connected) { 
        osDelay(TEN);
    }
    printf("g_connected: %d\r\n", g_connected);

    //获取网络接口
    g_iface = netifapi_netif_find("wlan0");
    if (g_iface) {
        //启动DHCP
        err_t ret = netifapi_dhcp_start(g_iface);
        printf("netifapi_dhcp_start: %d\r\n", ret);

        //等待DHCP服务器给我IP
        osDelay(ONE_HUNDRED); 
        //获取DHCP客户端信息
        ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
        printf("netifapi_netif_common: %d\r\n", ret);
    }
    return netId;
}

/*断开与AP的连接*/
void DisconnectWithHotspot(int netId)
{
    // 断开热点的连接
    if (g_iface) {
        err_t ret = netifapi_dhcp_stop(g_iface);
        printf("netifapi_dhcp_stop: %d\r\n", ret);
    }

    // 断开与AP的连接
    WifiErrorCode errCode = Disconnect(); 
    printf("Disconnect: %d\r\n", errCode);

    // 取消注册wifi事件
    errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
    printf("UnRegisterWifiEvent: %d\r\n", errCode);

    // 移除AP配置
    RemoveDevice(netId); 
    printf("RemoveDevice: %d\r\n", errCode);

    // 禁用wifi
    errCode = DisableWifi();
    printf("DisableWifi: %d\r\n", errCode);
}

